<?php

namespace Frontend\Controllers;

use Forms\ContactForm;
use Models\Informations;
use Models\Supports;

class HelpController extends ControllerBase {

    public function indexAction() {
        
    }

    public function aboutAction() {
        $about = "";
        $about_obj = Informations::findFirstById('about');
        if ($about_obj) {
            $about = $about_obj->value;
        }
        $this->view->about = $about;
    }

    public function securityAction() {
        $rules = "";
        $rules_obj = Informations::findFirstById('security_rules');
        if ($rules_obj) {
            $rules = $rules_obj->value;
        }
        $this->view->rules = $rules;
    }

    public function guideAction() {
        $guides = "";
        $guides_obj = Informations::findFirstById('user_guides');
        if ($guides_obj) {
            $guides = $guides_obj->value;
        }
        $this->view->guides = $guides;
    }

    public function contactAction() {
        $form = new ContactForm();
        if ($this->request->isPost()) {
            if ($form->isValid($this->request->getPost())) {
                $name = $this->request->getPost('name');
                $email = $this->request->getPost('email');
                $content = $this->request->getPost('content');
                $sendmail = $this->request->getPost('sendmail');
                $support = new Supports();
                $support->assign(array(
                    'name' => $name,
                    'email' => $email,
                    'content' => $content,
                    'created' => date('Y-m-d H:i:s'),
                    'resolved' => 0,
                ));
                if ($support->save()) {

                    if ($sendmail) {
                        $mail_params = array(
                            'name' => $name,
                            'content' => $content,
                        );
                        $mail_to = array(
                            $email => $name,
                        );
                        $this->getDI()->getMail()->send($mail_to, "Resend contact email", 'contact', $mail_params);
                    }
                }
                $form->appendMessage("Liên hệ của bạn đã được gửi, chúng tôi sẽ cố gắng phản hồi sớm nhất có thể", "contactsuccess", null);
            }
        }
        $this->view->form = $form;
    }

    public function moneyAction() {
        $money = "";
        $money_obj = Informations::findFirstById('user_money');
        if ($money_obj) {
            $money = $money_obj->value;
        }
        $this->view->money = $money;
    }

}
